package com.future.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.future.domain.Electric;
import com.future.domain.Water;
import com.future.domain.Waterdetails;
import com.future.dto.ElectricDto;
import com.future.dto.MsgDto;
import com.future.dto.WaterdetailDto;
import com.future.service.WaterService;
import com.future.service.WaterdetailsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author: 何晔
 * @time: 2022/6/29 15:05
 */
@RestController
@RequestMapping("Waterdetails")
public class WaterdetailsController {
    @Autowired
    private WaterdetailsService  waterdetailsService;

    @Autowired
    private WaterService waterService;


    @RequestMapping("select")
    public PageInfo<WaterdetailDto> select(WaterdetailDto waterdetailDto, int roomid){


        PageHelper.startPage(waterdetailDto.getCurrent(),waterdetailDto.getSize());
        List list    =   this.waterdetailsService.selectWaterdetails(roomid);

        PageInfo pageInfo=new PageInfo(list);
        return pageInfo;
    }

 @RequestMapping("pay")
     public MsgDto pay(WaterdetailDto waterdetailDto){
         Waterdetails waterdetails=new Waterdetails();
         waterdetails.setWaterdetailsroomid(waterdetailDto.getWaterdetailsroomdid());

         waterdetails.setPayeeman("天坑坑有限公司");
         waterdetails.setRechargename(waterdetailDto.getRechargename());
         waterdetails.setRechargeamount(waterdetailDto.getRechargeamount());
         waterdetails.setRechargedate(new Date());
         waterdetails.setRechargemethod("微信支付");



        this.waterdetailsService.save(waterdetails);

         // 根据 宿舍 id  获取  宿舍水费对象
         QueryWrapper queryWrapper = new QueryWrapper();
         queryWrapper.eq("waterroomid",waterdetailDto.getWaterdetailsroomdid());
         Water water =this.waterService.getOne(queryWrapper);

         water.setRemainingwater(water.getRemainingwater()+waterdetailDto.getRechargeamount());

           this.waterService.updateById(water);
          if(water.getRemainingwater()>0){
           water.setWaterstatus("正常使用");
              this.waterService.updateById(water);
           }




         return new MsgDto(true,"充值成功");
     }


}
